//
//  UIView+Extension.swift
//  AmountBody
//
//  Created by Qiu Jishuai on 2018/1/25.
//  Copyright © 2018年 RCMTM. All rights reserved.
//

import Foundation

enum DistributeSubviewsType: Int {
    case autoLayout
    case frame
}


extension Int {
    var insets: UIEdgeInsets {
        let spacing = CGFloat(self)
        return UIEdgeInsets(top: spacing, left: spacing, bottom: spacing, right: spacing)
    }
}

extension UIView {

    func distributeSquareSubviews(_ subviews: [UIView],
                                  leadSpacing: CGFloat = 30,
                                  tailSpacing: CGFloat = 30,
                                  itemSpacing: CGFloat = 10,
                                  centerYOffset: CGFloat = 0,
                                  type: DistributeSubviewsType = .frame) {
        guard subviews.count > 1 else {
            assert(false, "subviews的数量需要大于1, 实际count为\(subviews.count)")
            return
        }
        switch type {
        case .autoLayout:
            for view in subviews {
                view.mas_remakeConstraints({ (make) in
                    make?.centerY.equalTo()(self)?.offset()(centerYOffset)
                    make?.height.equalTo()(view.mas_width)
                })
            }
            (subviews as NSArray).mas_distributeViews(along: .horizontal, withFixedSpacing: itemSpacing, leadSpacing: leadSpacing, tailSpacing: tailSpacing)
        case .frame:
            let count = CGFloat(subviews.count)
            let width = round((frame.width - leadSpacing - tailSpacing - (count - 1) * itemSpacing) / count)
            for (index, view) in subviews.enumerated() {
                let x = round(leadSpacing + (itemSpacing + width) * CGFloat(index))
                view.frame = CGRect(x: x, y: 0, width: width, height: width)
                view.centerY = bounds.height / 2 + centerYOffset
            }
            break
        }
    }

    func distributeSubviews(_ subviews: [UIView],
                            insets: UIEdgeInsets = .zero,
                            itemSpacing: CGFloat = 5,
                            type: DistributeSubviewsType = .frame) {
        guard subviews.count > 1 else {
            assert(false, "subviews的数量需要大于1, 实际count为\(subviews.count)")
            return
        }

        switch type {
        case .autoLayout:
            for view in subviews {
                view.mas_remakeConstraints({ (make) in
                    make?.top.equalTo()(self)?.offset()(insets.top)
                    make?.bottom.equalTo()(self)?.offset()(-insets.bottom)
                })
            }
            (subviews as NSArray).mas_distributeViews(along: .horizontal, withFixedSpacing: itemSpacing, leadSpacing: insets.left, tailSpacing: insets.right)
        case .frame:
            let count = CGFloat(subviews.count)
            for (index, view) in subviews.enumerated() {
                let width = (frame.width - insets.left - insets.right - (count - 1) * itemSpacing) / count
                let x = insets.left + (itemSpacing + width) * CGFloat(index)
                let y = insets.top
                let height = frame.height - insets.top - insets.bottom
                view.frame = CGRect(x: x, y: y, width: width, height: height)
            }
            break
        }
    }
}

extension UIView {
    class func instanteFormNib<T: UIView>() -> T {
        let name = String(describing: T.self)
        let view = UINib(nibName: name, bundle: nil).instantiate(withOwner: nil, options: nil).last
        return view as! T
    }
}


extension UIView {
    @IBInspectable public var borderColor: UIColor? {
        get {
            guard let color = layer.borderColor else {
                return nil
            }
            return UIColor(cgColor: color)
        }
        set {
            guard let color = newValue else {
                layer.borderColor = nil
                return
            }
            layer.borderColor = color.cgColor
        }
    }

    @IBInspectable  var borderWidth: CGFloat {
        get {
            return layer.borderWidth
        }
        set {
            layer.borderWidth = newValue
        }
    }

    @IBInspectable public var cornerRadius: CGFloat {
        get {
            return layer.cornerRadius
        }
        set {
            layer.masksToBounds = true
            layer.cornerRadius = abs(CGFloat(Int(newValue * 100)) / 100)
        }
    }
}
extension UIView {

    enum Position {
        case top
        case left
        case bottom
        case right
    }

    ///添加分割线
    @discardableResult
    func addLine(on position: Position,
                 color: UIColor = UIColor.gray,
                 lineWidth: CGFloat = 1 / UIScreen.main.scale,
                 inset: UIEdgeInsets = UIEdgeInsets.zero) -> UIView {
        let line = UIView()
        line.backgroundColor = color
        addSubview(line)
        line.mas_remakeConstraints{ (make) in
            switch position {
            case .top:
                make?.left.equalTo()(self)?.offset()(inset.left)
                make?.right.equalTo()(self)?.offset()(-inset.right)
                make?.top.equalTo()(self)?.offset()(inset.top)
                make?.height.equalTo()(lineWidth)
            case .left:
                make?.left.equalTo()(self)?.offset()(inset.left)
                make?.top.equalTo()(self)?.offset()(inset.top)
                make?.bottom.equalTo()(self)?.offset()(-inset.bottom)
                make?.width.equalTo()(lineWidth)
            case .bottom:
                make?.left.equalTo()(self)?.offset()(inset.left)
                make?.right.equalTo()(self)?.offset()(-inset.right)
                make?.bottom.equalTo()(self)?.offset()(-inset.bottom)
                make?.height.equalTo()(lineWidth)
            case .right:
                make?.right.equalTo()(self)?.offset()(-inset.right)
                make?.top.equalTo()(self)?.offset()(inset.top)
                make?.bottom.equalTo()(self)?.offset()(-inset.bottom)
                make?.width.equalTo()(lineWidth)
            }
        }
        return line
    }

    //设置UI规范的圆角
    func setThemeRoundedBorder() {
        setRoundedBorder(withCornerRadius: 8,
                         borderColor: UIColor.gray,
                         borderWidth: 1 / UIScreen.main.scale)
    }

    ///设置圆角
    func setRoundedBorder(withCornerRadius cornerRadius: CGFloat = 0,
                          borderColor: UIColor = UIColor.white,
                          borderWidth: CGFloat = 0,
                          layerFrame: CGRect = CGRect.zero,
                          shouldRasterize: Bool = false,
                          useNewLayer: Bool = false) {
        let layer: CALayer
        if useNewLayer {
            layer = CALayer()
            var borderFrame = layerFrame
            if layerFrame == CGRect.zero {
                borderFrame = self.bounds
            }
            layer.frame = borderFrame
        } else {
            layer = self.layer
        }
        layer.cornerRadius = (cornerRadius == 0) ? layer.frame.width / 2 : cornerRadius
        layer.borderWidth = borderWidth
        layer.masksToBounds = true
        layer.borderColor = borderColor.cgColor
        if shouldRasterize {
            layer.shouldRasterize = shouldRasterize
            layer.rasterizationScale = UIScreen.main.scale
        }
        if useNewLayer {
            self.layer.addSublayer(layer)
        }
    }
}
